/*
 * History.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.history;

import android.content.Context;
import android.database.Cursor;
import de.studiorutton.gerengdic.LearningWordsApplication;

public class History {
	private static History INSTANCE = null;
	
	private HistoryProvider hp;
	private HistoryCursorAdapter hca;
	private Cursor cursor;

	public static History get() {
		if ( INSTANCE == null )
			INSTANCE = new History();
		return INSTANCE;
	}
	
	public static void release() {
		INSTANCE = null;
	}
	
	public HistoryProvider getHistoryProvider() {
		if (hp == null)
			hp = new HistoryProvider(LearningWordsApplication.get());
		return hp;
	}

	public HistoryCursorAdapter getHistoryCursorAdapter(Context context) {
		if (cursor != null)
			cursor.close();
		cursor = getHistoryProvider().query();
		hca = new HistoryCursorAdapter( context, cursor );
		return hca;
	}

	public void closeCursor() {
		cursor.close();
	}
	
	public void clearHistory() {
		hp.clearDatabase();
		if ( cursor != null )
			cursor.close();
		cursor = getHistoryProvider().query();
		hca.changeCursor( cursor );
		hca.notifyDataSetInvalidated();		
	}
}
